#include "StdAfx.h"
#include "LUA_Button.h"
#include "../gui/MButton.h"
#include "../gui/GUI_Manager.h"
#include "../kernel.h"

extern Kernel* g_kernel;

LUA_Button::LUA_Button(lua_State *L) : LUA_Component(L)
{
    // retrieve the name from lua
    std::string name   = luaL_checkstring( L, 1 );
    // retrieve the potential owner
    std::string sowner = luaL_checkstring( L, 2 );
    const gui::Component* owner = gui::GUI_Manager::GetInstance()->GetParentComponent( sowner );
    
    m_comp = new gui::MButton( owner ); // might be NULL
    m_comp->SetName( name );

    gui::GUI_Manager::GetInstance()->AddComponent( m_comp );

}

/** Set the text */
int LUA_Button::SetText( lua_State *L )
{
    if ( m_comp )
    {
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        // retrieve the name from lua
        std::string name   = luaL_checkstring( L, 1 );
        d->SetText( name );
    }
    return 0;

}
/** Get the text */
int LUA_Button::GetText( lua_State *L )
{
    if ( m_comp )
    {
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        lua_pushstring( L, d->GetText().c_str() ); 
        return 1;
    }
    return 0;

}

/** Add Functions to actions */
int LUA_Button::SetOnClick( lua_State *L )
{
    if ( m_comp )
    {
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        // retrieve the name from lua
        std::string name   = luaL_checkstring( L, 1 );
        d->SetOnClick( name );
    }
    return 0;
}

int LUA_Button::OnClick( lua_State *L )
{
    if ( m_comp )
    {
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        d->OnClick();
    }
    return 0;
}



/** Set background Image */
int LUA_Button::SetBackgroundImage( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the image name from lua
        std::string imageName   = luaL_checkstring( L, 1 );
        int fps  = luaL_checknumber( L, 2 );
        int amount = luaL_checknumber( L, 3 );
        int row = luaL_checknumber( L, 4 );
        int col = luaL_checknumber( L, 5 );

        Image image = g_kernel->GetResource()->LoadImages( imageName, row, col );
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        d->SetBackgroundImage( image, fps, amount, row, col );
    }
    return 0;
}

/** Set background Image */
int LUA_Button::SetHoverImage( lua_State *L )
{
    if ( m_comp )
    {
        // retrieve the image name from lua
        std::string imageName   = luaL_checkstring( L, 1 );
        int fps  = luaL_checknumber( L, 2 );
        int amount = luaL_checknumber( L, 3 );
        int row = luaL_checknumber( L, 4 );
        int col = luaL_checknumber( L, 5 );

        Image image = g_kernel->GetResource()->LoadImages( imageName, row, col );
        gui::MButton* d = static_cast<gui::MButton*>( m_comp );
        d->SetHoverImage( image, fps, amount, row, col );
    }
    return 0;
}

// -- Bind to LUA
#define method(class, name) {#name, &class::##name}

const char LUA_Button::className[] = "GUI_Button";
Lunar<LUA_Button>::RegType LUA_Button::methods[] = {
        method(LUA_Button, Create),
        method(LUA_Button, Destroy),
        method(LUA_Button, SetName),
        method(LUA_Button, GetName),
        method(LUA_Button, SetCaption),
        method(LUA_Button, GetCaption),
        method(LUA_Button, SetLocation),
        method(LUA_Button, GetLocation),
        method(LUA_Button, SetSize),
        method(LUA_Button, GetWidth),
        method(LUA_Button, GetHeight),
        method(LUA_Button, SetBackgroundColor),
        method(LUA_Button, GetBackgroundColor),
        method(LUA_Button, SetAlpha),
        method(LUA_Button, GetAlpha),
        method(LUA_Button, SetFocus),
        method(LUA_Button, HasFocus),
        method(LUA_Button, SetForegroundColor),
        method(LUA_Button, GetForegroundColor),
        method(LUA_Button, SetVisible),
        method(LUA_Button, IsVisible),

        method(LUA_Button, SetText),
        method(LUA_Button, GetText),
        method(LUA_Button, SetOnClick),
        method(LUA_Button, OnClick),
        method(LUA_Button, SetBackgroundImage),
        method(LUA_Button, SetHoverImage),
        {NULL, NULL}
};
